package com.ruike.alisurface.ui.answerQuestion.http;

import com.ruike.alisurface.ui.answerQuestion.bean.AnswerQuestionBean;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_frame.base.BaseView;
import com.voodoo.lib_frame.httpTools.ApiTools;
import com.voodoo.lib_frame.httpTools.RequestParams;
import com.voodoo.lib_utils.GsonUtils;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Author: voodoo
 * CreateDate: 2023/11/22 022上午 09:16
 * Description: 通诺接口
 * http://showdoc.shouhuojiyun.com/web/#/161?page_id=3331
 */
public class TongNuoHttp {

    static final String BASE_URL = "https://rk.shouhuojiyun.com/otherapi/tongnuo/";
    public static final String CHECK_CAN_USE_URL = BASE_URL + "check_mch_can_use"; // 检查机器是否可用
    public static final String GET_PRODUCT_LIST_URL = BASE_URL + "get_product_list"; // 获取商品列表
    public static final String GET_QUESTION_LIST_URL = BASE_URL + "get_question"; // 获题目列表
    public static final String SUBMIT_ANSWER_URL = BASE_URL + "submit_answer"; // 提交题目
    public static final String CONFIRM_QUESTION_ORDER_URL = BASE_URL + "confirm_question_order"; // 确认订单完成减库存

    private static ApiTools apiTools = new ApiTools();

    /**
     * 检查此机器是否可以使用，将回传的url作为二维码内容创建二维码
     */
    public static void checkCanUse(BaseView baseView) {
        RequestParams params = new RequestParams();
        params.put("mchid", ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        String randomStr = String.valueOf((int) (Math.random() * 10000));
        ShareUtils.getInstance().putString(ShareKey.GET_DEVICE_CAN_USE_RANDOM_KEY, randomStr);
        params.put("nonce", randomStr);
        apiTools.getRequest(CHECK_CAN_USE_URL, params, baseView);
    }

    /**
     * 获取商品列表
     */
    public static void getProductList(BaseView baseView) {
        RequestParams params = new RequestParams();
        params.put("mchid", ShareUtils.getInstance().getString(ShareKey.MCH_ID));
        apiTools.getRequest(GET_PRODUCT_LIST_URL, params, baseView);
    }

    /**
     * 获取题目列表
     */
    public static void getQuestionList(BaseView baseView) {
        JSONObject params = new JSONObject();
        try {
            params.put("mch_id", ShareUtils.getInstance().getString(ShareKey.MCH_ID));
            params.put("strategy_id", ShareUtils.getInstance().getString(ShareKey.STRATEGY_ID_KEY));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        L.i("读取的策略ID：" + ShareUtils.getInstance().getString(ShareKey.STRATEGY_ID_KEY));
        apiTools.postJsonRequest(GET_QUESTION_LIST_URL, params.toString(), baseView);
    }

    /**
     * 提交作答题目
     */
    public static void submitAnswer(List<AnswerQuestionBean.AnswerListBean> answerList, BaseView baseView) {
        JSONObject params = new JSONObject();
        try {
            params.put("mchid", ShareUtils.getInstance().getString(ShareKey.MCH_ID));
//            params.put("question_openid", ShareUtils.getInstance().getString(ShareKey.OPEN_ID_KEY));
            params.put("answer_list", new JSONArray(GsonUtils.list2JsonArrayNotNulls(answerList)));
            params.put("start_time", ShareUtils.getInstance().getString(ShareKey.START_ANSWER_TIME));
            params.put("strategy_id", ShareUtils.getInstance().getString(ShareKey.STRATEGY_ID_KEY));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        apiTools.postJsonRequest(SUBMIT_ANSWER_URL, params.toString(), baseView);
    }

    /**
     * 订单完成
     */
    public static void questionEndOrder(String orderId, boolean isSuccess, String outParams, BaseView baseView) {
        RequestParams params = new RequestParams();
        params.put("order_id", orderId);
        params.put("is_end", isSuccess);
        params.put("out_params", outParams);
        apiTools.getRequest(CONFIRM_QUESTION_ORDER_URL, params, baseView);
    }

}
